package com.tinycloud.miracll.product.controller;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.core.utils.StringUtils;
import com.tinycloud.miracll.product.domain.NoModelExcelData;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tinycloud.common.log.annotation.Log;
import com.tinycloud.common.log.enums.BusinessType;
import com.tinycloud.common.security.annotation.PreAuthorize;
import com.tinycloud.miracll.product.domain.ProductLine;
import com.tinycloud.miracll.product.service.IProductLineService;
import com.tinycloud.common.core.web.controller.BaseController;
import com.tinycloud.common.core.web.domain.AjaxResult;
import com.tinycloud.common.core.utils.poi.ExcelUtil;
import com.tinycloud.common.core.web.page.TableDataInfo;

/**
 * 产线管理Controller
 * 
 * @author tiny
 * @date 2021-08-20
 */
@RestController
@RequestMapping("/productLine")
public class ProductLineController extends BaseController
{
    @Autowired
    private IProductLineService productLineService;

    /**
     * 查询产线管理列表
     */
    @PreAuthorize(hasPermi = "product:productLine:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductLine productLine)
    {
        startPage();
        List<ProductLine> list = productLineService.selectProductLineList(productLine);
        return getDataTable(list);
    }

    /**
     * 获取产线管理详细信息
     */
    @PreAuthorize(hasPermi = "product:productLine:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(productLineService.selectProductLineById(id));
    }

    /**
     * 新增产线管理
     */
    @PreAuthorize(hasPermi = "product:productLine:add")
    @Log(title = "产线管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductLine productLine)
    {
        return toAjax(productLineService.insertProductLine(productLine));
    }

    /**
     * 导出产线管理列表
     */
    @PreAuthorize(hasPermi = "product:productLine:export")
    @Log(title = "产线管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductLine productLine) throws IOException
    {
        List<ProductLine> list = productLineService.selectProductLineList(productLine);
        ExcelUtil<ProductLine> util = new ExcelUtil<ProductLine>(ProductLine.class);
        util.exportExcel(response, list, "产线管理数据");
    }
 /**
     * 导出产线scada点位采集数据
     */
    @PreAuthorize(hasPermi = "product:productLine:scadaPointExport")
    @Log(title = "产线管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/scadaDataExport")
    public void exportScadaData(HttpServletRequest request,HttpServletResponse response , String idList, Date beginTime,Date endTime,Integer count) throws IOException
    {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + "测试" + ".xlsx");
        String[] idArray = StringUtils.split(idList,"_");
        List<String> idsTem =  Arrays.asList(idArray);
        ExcelWriter excelWriter = null;
        List<NoModelExcelData> noModelExcelDataList = productLineService.exportLineScadaData(idsTem, beginTime,endTime,5,count);
        ServletOutputStream servletOutputStream = response.getOutputStream();
        try {
            excelWriter = EasyExcel.write(servletOutputStream).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            int i = 0;
            for (NoModelExcelData noModelExcelData: noModelExcelDataList) {
                String sheetName = noModelExcelData.getSheetName();
                List<List<String>> headList = noModelExcelData.getHeadList();
                List<List<String>> dataList = noModelExcelData.getDataList();
                WriteSheet writeSheet = EasyExcel.writerSheet(i, sheetName).head(headList).build();
                excelWriter.write(dataList, writeSheet);
                i++;
            }
            System.out.println("end exprot...");
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }

    }


    /**
     * 修改产线管理
     */
    @PreAuthorize(hasPermi = "product:productLine:edit")
    @Log(title = "产线管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductLine productLine)
    {
        return toAjax(productLineService.updateProductLine(productLine));
    }

    /**
     * 删除产线管理
     */
    @PreAuthorize(hasPermi = "product:productLine:remove")
    @Log(title = "产线管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(productLineService.deleteProductLineByIds(ids));
    }
}
